#!/bin/sh

#
#  Settings from configure/make install
#
PREFIX="@PREFIX@"
DESTDIR="@DESTDIR@"
VADDIR="@VADDIR@"
DBDIR="@DBDIR@"
DEMODIR="@DEMODIR@"
HOSTINGDIR="@HOSTINGDIR@"
HTTPROOTDIR="@HTTPROOTDIR@"

#
#  Argument
#
DB=${1-database}


#
#  Calculate hostname
#
HOST=`hostname | tr '[a-z]' '[A-Z]' | sed -e 's/\..*$//' 2>/dev/null`
if test "x$HOST" = "x"
then
    HOST=localhost
fi


case $DB in
	database)
		PORT=1111
		HTTP_PORT=8890
		DBNAME=virtuoso
		DIR="$DBDIR"
		REPLNAME="db-$HOST"
		;;

	demo)
		PORT=1112
		HTTP_PORT=8891
		DBNAME=demo
		DIR="$DEMODIR"
		REPLNAME="demo-$HOST"
		;;

	*)
		echo "Unknown database name [$DB]"
		exit 1
		;;
esac


#
#  Generate proper ini file
#

if test -f "${DESTDIR}${DIR}/${DBNAME}.ini"
then
	INIFILE="${DESTDIR}${DIR}/${DBNAME}.ini.sample"
else
	INIFILE="${DESTDIR}${DIR}/${DBNAME}.ini"
fi
echo "Installing default ini file as $INIFILE"

sed	-e "s,[@]VADDIR[@],$VADDIR,g" \
	-e "s,[@]HOSTINGDIR[@],$HOSTINGDIR,g" \
	-e "s,[@]DBDIR[@],$DBDIR,g" \
	-e "s,[@]DBNAME[@],$DBNAME,g" \
	-e "s,[@]HOST[@],$HOST,g" \
	-e "s,[@]PORT[@],$PORT,g" \
	-e "s,[@]HTTP_PORT[@],$HTTP_PORT,g" \
	-e "s,[@]HTTP_ROOTDIR[@],$HTTPROOTDIR,g" \
	-e "s,[@]REPLNAME[@],$REPLNAME,g" \
	-e "s,[@]ZNAME[@],$DBNAME ($HOST),g" \
        -e 's,[@]REM_IM[@],@REM_IM@,g' \
        -e 's,[@]REM_HSL[@],@REM_HSL@,g' \
        -e 's,[@]REM_WBXML2[@],@REM_WBXML2@,g' \
        -e 's,[@]REM_PHP5[@],@REM_PHP5@,g' \
        -e 's,[@]REM_PYTHON[@],@REM_PYTHON@,g' \
        -e 's,[@]REM_PERL[@],@REM_PERL@,g' \
        -e 's,[@]REM_RUBY[@],@REM_RUBY@,g' \
        -e 's,[@]REM_PROJ4[@],@REM_PROJ4@,g' \
        -e 's,[@]REM_GEOS[@],@REM_GEOS@,g' \
        -e 's,[@]REM_SHAPEFILEIO[@],@REM_SHAPEFILEIO@,g' \
        -e 's,[@]REM_RUBY[@],@REM_RUBY@,g' \
        -e 's,[@]REM_GRAPHQL[@],@REM_GRAPHQL@,g' \
	< default-virtuoso.ini > "$INIFILE"

exit 0
